namespace Math
{
    using System;
    public class Complex
    {
        public Complex()
        {
        }
        public Complex(double real, double imag)
        {
            re_ = real;
            im_ = imag;
        }

        // Accessor prototype matches STL complex template
        public double real() { return re_; }
        public double imag() { return im_; }

        // Square of the magnitude
        public double magnitude2() { return re_ * re_ + im_ * im_; }
        // Magnitude
        public double magnitude()  { return Math.Sqrt(magnitude2()); }

        public static Complex operator +(Complex lhs, Complex rhs)
        {
            return (new Complex(lhs.re_ + rhs.re_, lhs.im_ + rhs.im_));
        }
        public static Complex operator -(Complex lhs)
        {
            return (new Complex(-lhs.re_, -lhs.im_));
        }
        public static Complex operator -(Complex lhs, Complex rhs)
        {
            return (new Complex(lhs.re_ - rhs.re_, lhs.im_ - rhs.im_));
        }
        public static Complex operator *(Complex lhs, Complex rhs)
        {
            return (new Complex(lhs.re_ * rhs.re_ - lhs.im_ * rhs.im_, lhs.re_ * rhs.im_ + rhs.re_ * lhs.im_));
        }
        public static Complex operator /(Complex lhs, Complex rhs)
        {
            double c1, c2, d;
            d = rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_;
            if (d == 0)
            {
                return (new Complex(0, 0));
            }
            c1 = lhs.re_ * rhs.re_ + lhs.im_ * rhs.im_;
            c2 = lhs.im_ * rhs.re_ - lhs.re_ * rhs.im_;
            return (new Complex(c1 / d, c2 / d));
        }
        public override string ToString()
        {
            return (String.Format("{0} + {1}i)", re_, im_));
        }

        // Member variables
        private double re_;
        private double im_;
    }
}
  